package com.naiterui.ehp.bs.doctor.controller;

import javax.servlet.http.HttpServletRequest;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bs.doctor.service.AdVersionService;
import com.naiterui.ehp.bs.doctor.vo.VersionResultVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.VERSION_CONTROL})
@Slf4j
@Controller
@RequestMapping("ad")
@RequiredArgsConstructor
public class AdVersionController {

    private final AdVersionService adVersionService;

    @ApiOperation(value = "检查版本", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "version", value = "当前版本", dataType = "String", paramType = "form", required = true),
            @ApiImplicitParam(name = "paltform", value = "平台(android、iphone)", dataType = "String", paramType = "form", required = true),
    })
    @RequestMapping("checkVersion")
    @ResponseBody
    public VersionResultVo versionCheck(String version, String paltform, String newVer, HttpServletRequest request)
        throws BusinessException {
        log.info("检查更新 version:{}, paltform:{}, newVer:{}", version, paltform, newVer);
        // 优先使用header中的值
        String hVersion = request.getHeader(HeaderConstant.HEADER_VERSION);
        String hPpaltform = request.getHeader(HeaderConstant.HEADER_PLATFORM);
        return adVersionService.versionCheck(version, paltform, newVer, hVersion, hPpaltform);
    }

    @RequestMapping("saveVersion")
    @ResponseBody
    @ApiIgnore
    public void save(Integer appPlat, String lastVerNum, String lastVerSize, Integer result, String forceVersion,
        String content, String url) throws BusinessException {
        log.info("输入平台:" + appPlat + ",最新版本号:" + lastVerNum + ",最新版本大小:" + lastVerSize + ",被更新版本号:" + forceVersion
            + ",更新提示语:" + content + ",更新跳转地址:" + url);
        adVersionService.saveVersion(appPlat, lastVerNum, lastVerSize, result, forceVersion, content, url);
    }

    @RequestMapping("saveH5Version")
    @ResponseBody
    @ApiIgnore
    public void saveH5(String lastVerNum) {
        log.info("输入h5平台最新版本号:" + lastVerNum);
        adVersionService.saveH5Version(lastVerNum);
    }
}
